package com.qiwx.pigeon.helper;

import android.util.Log;

import com.google.gson.Gson;
import com.qiwx.pigeon.Utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @description
 * @Author qiwx
 * @Date 2019-12-25 10:00
 * 日志帮助类
 **/
public class LogHelper {

    public static void main(String[] args) {

        Gson gson=new Gson();

        Map<String, Object> map=new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");

        System.out.println(new Gson().toJson(map));
    }

    private String requestUrl;//请求地址
    private Map<String, String> requestParam;//请求参数
    private String responseBody;//返回数据
    private String responseTime;//相应时间

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public Map<String, String> getRequestParam() {
        return requestParam;
    }

    public void setRequestParam(Map<String, String> requestParam) {
        this.requestParam = requestParam;
    }

    public String getResponseBody() {
        return responseBody;
    }

    public void setResponseBody(String responseBody) {
        this.responseBody = responseBody;
    }

    public String getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static void printLine(String tag, boolean isTop) {
        if (isTop) {
            Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
        } else {
            Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
        }
    }
    public static void printJson(String tag, String msg, String headString) {

        String message;

        try {
            if (msg.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(msg);
                message = jsonObject.toString(4);//最重要的方法，就一行，返回格式化的json字符串，其中的数字4是缩进字符数
            } else if (msg.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(msg);
                message = jsonArray.toString(4);
            } else {
                message = msg;
            }
        } catch (JSONException e) {
            message = msg;
        }

        printLine(tag, true);
        message = headString + LINE_SEPARATOR + message;
        String[] lines = message.split(LINE_SEPARATOR);

        StringBuilder sb = new StringBuilder();
        for (String line : lines) {
            line=line.replaceAll("\\\\","");
//            LogUtil.e(tag, "║ " + line); 这样打出来每行前面都有TAG 不方便复制出参数
            sb.append(line+"\n");
        }
        String str = sb.toString();
        LogUtil.e(tag, "║====== " + str);
        printLine(tag, false);
    }

    public void printInfo(){
        Map responseMap=null;
        if(this.responseBody!=null&&this.responseBody.length()>0){
            Gson gson=new Gson();
            try{
                responseMap= gson.fromJson(responseBody, Map.class);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(requestParam==null)
            requestParam=new HashMap<>();
        if(responseMap!=null){
            requestParam.putAll(responseMap);
        }
        requestParam.put("requestUrl",requestUrl);
        requestParam.put("responseTime",responseTime);
        String info=new Gson().toJson(requestParam);
        info=info.replaceAll("\\\\","");
        printJson("TAG",info,"Request");
//        Log.e("Request",info) ;
    }


}
